var coordinateLineColor="#010101";
var baseLineColor="#c0c0c0";
var wordColor="#12aadf";
var tesLineColor="#000000";
var level1Color="#1278fa";
var level2Color="#12fa66";
var level3Color="#cccc12";
var level4Color="#fa1212";
var levelcolors=[level1Color,level2Color,level3Color,level4Color];


//绘制坐标函数(数据分析的坐标)
function paintCoordinate(canvas){
    var ctx = canvas.getContext('2d');
    ctx.lineWidth=1;
    ctx.strokeStyle=coordinateLineColor;
    ctx.fillStyle=wordColor;
    ctx.textBaseline="middle";
    ctx.font=' 16 16px , sans-serif'
    ctx.textAlign="left";
    //horizontal coordinate
    ctx.beginPath();
    ctx.moveTo(150,canvas.height-50);
    ctx.lineTo(canvas.width-50,canvas.height-50);
    ctx.stroke();
    ctx.save();

    //vertical coordinate
    ctx.beginPath();
    ctx.lineWidth=1;
    ctx.moveTo(150,canvas.height-50);
    ctx.lineTo(150,50);
    ctx.stroke();
    ctx.save();

    //vertical line base
    var vlength=canvas.height-100-25
    var dvlength=vlength/4
    var startY=canvas.height-50;
    ctx.strokeStyle=baseLineColor;
    ctx.fillText(0,110,startY)
    for(var a=0;a<4;a++){
        ctx.strokeStyle=levelcolors[a];
        ctx.beginPath();
        ctx.moveTo(150,startY-dvlength*(a+1));
        ctx.lineTo(canvas.width-50,startY-dvlength*(a+1));
        ctx.stroke();
        ctx.save();
        ctx.fillText((a+1)*0.25,110,startY-dvlength*(a+1))
        if(a==0){
            ctx.fillText("正常",10,startY-dvlength*(a+1))
        }
        else if(a==1){
            ctx.fillText("轻度结焦",10,startY-dvlength*(a+1))
        }
        else if(a==2){
            ctx.fillText("结焦较严重",10,startY-dvlength*(a+1))
        }
        else{
            ctx.fillText("严重结焦",10,startY-dvlength*(a+1))
        }
    }
    ctx.save();


}

/*绘制坐标函数（最新数据显示的坐标）*/
//真实值显示没写??
//画布顶到纵线顶为50，纵线顶到1100刻度为150，纵线低到画布底为50，原点到700为100
function paintCoordinate1(canvas,group,offsets){
    var ctx = canvas.getContext('2d');
    ctx.lineWidth=1;
    ctx.fillStyle=wordColor;
    ctx.textBaseline="middle";
    ctx.font=' 20 25px , sans-serif'
    ctx.textAlign="end";
    ctx.textBaseline="middle";
    ctx.textAlign="left";

    //vertical line base
    var vlength=canvas.height-100-150
    var startY=canvas.height-50;
    var startX=150;
    var endX=canvas.width-50;
    var posistion=startY-100;
    var secondVLength=vlength-100;//900到1100的像素长度
    var count = 2;
    var dvlength=secondVLength/count;//像素每刻度
    var tempos=700;
    var hlength=endX-startX;
    var hcount=12;
    var dhlenth=hlength/hcount;


    ctx.save();
    ctx.strokeStyle="#777777";//灰色
    ctx.fillText("0℃",50,startY)
    for(var a=0;a<count+1;a++){
//        ctx.strokeStyle=levelcolors[a+1];
        ctx.beginPath();
        ctx.moveTo(150,posistion);
        ctx.lineTo(canvas.width-50,posistion);
        ctx.stroke();
//        ctx.save();
        ctx.fillText(String(tempos)+"℃",50,posistion)
        tempos+=200;
        posistion-=dvlength;
    }
    ctx.closePath();
//    ctx.save();

    /*12根管表示*/
    for(var a=0;a<hcount;a++){
        ctx.fillText("管"+String(a+1+(group-1)*12),startX+a*dhlenth+offsets,startY+20);
    }

    //绘制坐标
//    drawXY(canvas);
    ctx.save();
    ctx.lineWidth=1;
    ctx.strokeStyle="#000000";
    //horizontal coordinate
    ctx.beginPath();
    ctx.moveTo(150,canvas.height-50);
    ctx.lineTo(canvas.width-50,canvas.height-50);
    ctx.stroke();//ctx.closePath();??应该是多余的吧
    ctx.closePath();
//    ctx.save();

    //vertical coordinate
    ctx.beginPath();
    ctx.lineWidth=1;
    ctx.moveTo(150,canvas.height-50);
    ctx.lineTo(150,50);
    ctx.stroke();
    ctx.closePath();
}

/*绘制线性图，12根管的数据*/
function paintLinearLines(canvas,datas,colors){
    var ctx = canvas.getContext('2d');
    var vlength=canvas.height-100-150
    var startY=canvas.height-50;
    var startX=150;
    var endX=canvas.width-50;
    var posistion=startY-100//700度位置
    var secondVLength=vlength-100;
    var count = 2;
    var dvlength=secondVLength/count;
    var tempos=700;
    var hlength=endX-startX;
    var hcount=12;
    var dhlenth=hlength/hcount;

    /*线条*/
    ctx.strokeStyle=colors[0];
    ctx.fillStyle=colors[1];
    ctx.lineWidth=1;

    /*resovle datass*/
    for(var a=0;a<datas.length;a++){
//        console.log("test datas",datas[a]);
        //??算法的结果是什么(值在1100-900之间占的百分比*1100-900间的像素值)
        datas[a]=Math.round((posistion-(datas[a]-700)*dvlength*2/400));
    }

    /*绘制体*/
    //纵坐标线为画布高-100;
    ctx.beginPath();
    ctx.moveTo(startX,startY);
    ctx.lineTo(startX,datas[0]);
    for(index=1;index<datas.length;index++){
        //??曲线算出来的结果也不明白起点moveto,两个拉点，最后终点，总共3个点，6个参数
        ctx.bezierCurveTo(startX+dhlenth*(index-1)+dhlenth/3,datas[index-1],
                          startX+dhlenth*index-dhlenth/3,datas[index]
                          ,startX+dhlenth*index,datas[index]);
//        ctx.lineTo(startX+dhlenth*index,datas[index]);
    }
    ctx.lineTo(startX+dhlenth*(datas.length-1),startY);
    //不能在这插入ctx.stroke();吗??
    ctx.closePath();
    ctx.fill();
//    ctx.save();

    /*绘制线*/

    ctx.beginPath();
    ctx.moveTo(startX,datas[0]);
    var index=0;
    for(index=1;index<datas.length;index++){
        ctx.bezierCurveTo(startX+dhlenth*(index-1)+dhlenth/3,datas[index-1],
                          startX+dhlenth*index-dhlenth/3,datas[index]
                          ,startX+dhlenth*index,datas[index]);
//        ctx.lineTo(startX+dhlenth*index,datas[index])
//        console.log("test datas",datas[index]);
    }
    ctx.stroke();
    ctx.closePath();

//    ctx.save();
//    console.log("starty",startY,"canvas height",canvas.height);
}

/*绘制直方图，12根管数据*/
function paintRectTable(canvas,datas,colors,indexFlags){
//    console.log("debug datas :","painting rect datas");
    var ctx = canvas.getContext('2d');

    ctx.save();
    var vlength=canvas.height-100-150
    var startY=canvas.height-50;
    var startX=150;
    var endX=canvas.width-50;
    var posistion=startY-100
    var secondVLength=vlength-100;
    var count = 2;
    var dvlength=secondVLength/count;
    var tempos=700;
    var hlength=endX-startX;
    var hcount=12;
    var dhlenth=hlength/hcount;

    var rectWidth=dhlenth/4;
    var rectHeight=0;
    var fontsize=rectWidth/2;//??不太清楚，三个数字不应该/3吗，不过2确实更适合
    ctx.font=String(Math.round(fontsize))+" "+String(Math.round(fontsize))+"px , sans-serif"

    /*线条*/
    ctx.strokeStyle=colors[0];
    ctx.fillStyle=colors[1];
    ctx.lineWidth=1;

    var preDatas=[0,0,0,0,0,0,0,0,0,0,0,0];
    for(var i=0;i<12;i++){
        preDatas[i]=Math.round(datas[i]);
    }

    /*resovle datass*/
    for(var a=0;a<datas.length;a++){
        datas[a]=Math.round((posistion-(datas[a]-700)*dvlength*2/400));
    }

    for(var a=0;a<datas.length;a++){
        rectHeight=startY-datas[a];
        ctx.fillRect(startX+dhlenth*a+rectWidth*(indexFlags-1),startY-rectHeight,rectWidth,rectHeight);
        ctx.fillText(preDatas[a],startX+dhlenth*a+rectWidth*(indexFlags-1)+2,startY-rectHeight-20)
    }


}


//绘制线段函数
function drawTestLine(canvas,modelObject){
    var ctx = canvas.getContext('2d');
    //重新绘制
    ctx. clearRect(0,0,canvas.width,canvas.height);
    paintCoordinate(canvas);
    ctx.lineWidth=1;
    ctx.strokeStyle=tesLineColor;
    ctx.fillStyle=wordColor;
    ctx.textBaseline="middle";
    ctx.font=' 16 16px , sans-serif'
    ctx.textAlign="center";

    var dx_length=(canvas.width-150)/(modelObject.count-1);
    var yLength=canvas.height-100-25;
    var startX=150;
    var startY=canvas.height-50;

    var t1=800+Math.random()*20;
    var t0=800+Math.random()*20;
    var value=testFunc(modelObject.get(0).temp,modelObject.get(1).temp,t0,t1);


    ctx.beginPath();
    ctx.moveTo(startX,startY-yLength*value);
    var currentby=startY-yLength*value;

    //曲线
    for(var a=1;a<modelObject.count-1;a++){
        t1=800+Math.random()*20;
        t0=800+Math.random()*20;
        value=testFunc(modelObject.get(a).temp,modelObject.get(a+1).temp,t0,t1);
//        ctx.lineTo(startX+dx_length*a,startY-yLength*value);
        ctx.bezierCurveTo((startX+dx_length*(a-1)+dx_length/3),currentby
                          ,(startX+dx_length*a-dx_length/3),startY-yLength*value,
                          startX+dx_length*a,startY-yLength*value);
        currentby=startY-yLength*value;
    }
    ctx.lineWidth=3;
    ctx.stroke();
    ctx.closePath();

    //时间
    for(var i=0;i<modelObject.count-1;i=i+(modelObject.count/21+1)){
        ctx.fillText(modelObject.get(i+1).time,startX+dx_length*i,startY+30);
    }




    ctx.save();
    canvas.requestPaint();
}

/**
  *诊断函数
  *v0->out0/in0
  *v1->out1/in1
  *t0->cot0
  *t1->cot1
**/
function testFunc(v0,v1,t0,t1){
    var p=(v1-t1)/(v0-t0)-1;
//    return p;
    if(p>=0){
        return p;
    }
    else{
        return 0;
    }
}
